<?php

	// Split incoming POST string by comma
	$dataArray = array();
	$dataArray = explode(",", $_POST['status']);

	perform_action($dataArray);

	function perform_action($array) {

		// Retrieve machine, first element of POST
		$machine = $array[0];

		$filenames = array();
		// Get all other elements of POST, add to filenames array
		for($i = 1; $i < count($array); $i++) {
			array_push($filenames, $array[$i]);
		}

		// Iteratie filenames, call insert on each file
		for($i = 0; $i < count($filenames); $i++) {
			insert_available($machine, $filenames[$i]);
		}
	}

	function insert_available($machine, $filename) {
		$con = mysqli_connect("localhost", "discovery_user", "hallo1", "mm_discovery");

		if(mysqli_connect_errno()) {
			echo "Kan niet verbinden: " . mysqli_connect_error();
		}

		$query = "INSERT INTO available (machine, filename) VALUES ('" . $machine . "', '" . $filename . "')";

		mysqli_query($con, $query);
		mysqli_close($con);
	}



?>